<template>
  <div>
    <!-- 处方开具主要功能 -->
    <div>
      <el-row :gutter="20">
        <!-- 患者列表 -->
        <el-col :span="12">
          <el-card shadow="hover">
            <div>
              <span>待查患者</span>
              <el-table
                v-loading="loading"
                :data="patientData"
                stripe
                border
                :default-sort="{ prop: 'patient_id', order: 'asc' }"
                max-height="250"
                min-height="250"
                element-loading-text="正在加载..."
                element-loading-spinner="el-icon-loading"
                element-loading-background="rgba(0, 0, 0, 0.8)"
                style="width: 575px"
              >
                <el-table-column align="center" prop="patient_id" label="ID" width="100" />
                <el-table-column align="center" prop="patient_name" label="姓名" width="150" />
                <el-table-column align="center" prop="patient_age" label="年龄" width="100" />
                <el-table-column align="center" prop="patient_gender" label="性别" width="100" />
                <el-table-column align="center" fixed="right" label="操作" width="120">
                  <template slot-scope="scope">
                    <el-tooltip class="item" effect="dark" content="选择此病人，并开立处方" placement="right">
                      <el-button type="primary" size="mini" @click="choosePatient(scope.row)">选择</el-button>
                    </el-tooltip>

                  </template>
                </el-table-column>
              </el-table>
            </div>
          </el-card>
        </el-col>

        <!-- 当前患者信息 -->
        <el-col :span="12">
          <el-card shadow="hover">
            <el-empty v-if="!isChoose" description="暂未选择患者" />
            <div v-show="isChoose">
              <el-descriptions size="mini" title="患者基本信息" direction="vertical" :column="3" border>
                <el-descriptions-item :span="2"><template slot="label">
                  <i class="el-icon-user" />
                  姓名
                </template>{{ choosedPatient.patient_name }}</el-descriptions-item>
                <el-descriptions-item label="ID" :span="2">{{ choosedPatient.patient_id }}</el-descriptions-item>
                <el-descriptions-item :span="2"><template slot="label">
                  <i class="el-icon-mobile-phone" />
                  手机号
                </template>{{ choosedPatient.patientPhone }}</el-descriptions-item>
                <el-descriptions-item label="年龄" :span="2">{{ choosedPatient.patient_age }}</el-descriptions-item>
                <el-descriptions-item label="性别" :span="2">{{ choosedPatient.patient_gender }}</el-descriptions-item>
                <el-descriptions-item label="处方开具" :span="2">
                  <el-button size="mini" type="success" icon="el-icon-plus" @click="openAddPre" />
                </el-descriptions-item>
              </el-descriptions>

            </div>
          </el-card>
        </el-col>
      </el-row>

      <div>
        <el-divider content-position="center">患者所有处方表</el-divider>
      </div>

      <el-row :gutter="20">
        <el-col :span="24">
          <el-card shadow="hover">
            <el-empty v-if="!isChoose" description="暂未选择患者" />
            <div v-show="isChoose" style="width: 100%">
              <el-table
                :data="preList"
                stripe
                border
                :default-sort="{ prop: 'time', order: 'descending' }"
                element-loading-text="正在加载..."
                element-loading-spinner="el-icon-loading"
                element-loading-background="rgba(0, 0, 0, 0.8)"
                style="width: 100%"
              >
                <el-table-column align="center" prop="id" label="处方ID" width="75px" />
                <el-table-column align="center" prop="patient_id" label="患者ID" width="75px" />
                <el-table-column align="center" prop="doctor_id" label="医生ID" width="75px" />
                <el-table-column align="center" prop="time" label="处方时间" width="100px" />
                <el-table-column align="center" prop="description" label="描述备注" width="125px" />
                <el-table-column align="center" prop="status" label="状态" :formatter="preState" width="113px">
                  <template slot-scope="scope">
                    <el-tag v-if="scope.row.status !='已完成'" type="warning" size="mini">未完成</el-tag>
                    <el-tag v-if="scope.row.status === '已完成'" type="success" size="mini">已完成</el-tag>
                  </template>
                </el-table-column>
                <el-table-column align="center" label="操作" width="300px">
                  <template slot-scope="scope">
                    <el-button-group>
                      <div>
                        <el-button
                          v-if="scope.row.status !='已完成'"
                          style="color: #0796b8"
                          type="text"
                          size="mini"
                          :disabled="scope.row.status === '已完成'"
                          @click="openEditPre(scope.row)"
                        >基本编辑</el-button>
                        <el-button
                          v-else
                          type="text"
                          size="mini"
                          :disabled="scope.row.status === '已完成'"
                          @click="openEditPre(scope.row)"
                        >基本编辑</el-button>
                        <el-divider direction="vertical" />
                        <el-button
                          v-if="scope.row.status !='已完成'"
                          style="color: #42B983"
                          type="text"
                          size="mini"
                          :disabled="scope.row.status === '已完成'"
                          @click="preDetailEdit(scope.row)"
                        >用药调整</el-button>
                        <el-button
                          v-else
                          type="text"
                          size="mini"
                          :disabled="scope.row.status === '已完成'"
                          @click="preDetailEdit(scope.row)"
                        >用药调整</el-button>
                        <el-divider direction="vertical" />
                        <el-button
                          v-if="scope.row.status !='已完成'"
                          style="color: #FF6B37"
                          type="text"
                          size="mini"
                          :disabled="scope.row.status === '已完成'"
                          @click="preDetailDone(scope.row)"
                        >结束处方</el-button>
                        <el-button
                          v-else
                          type="text"
                          size="mini"
                          :disabled="scope.row.status === '已完成'"
                          @click="preDetailDone(scope.row)"
                        >结束处方</el-button>
                      </div>
                    </el-button-group>
                  </template>
                </el-table-column>
              </el-table>
            </div>
          </el-card>
        </el-col>
      </el-row>

      <div>
        <el-divider content-position="center">处方明细表</el-divider>
      </div>

      <el-row :gutter="20">
        <el-col :span="24">
          <el-card shadow="hover">
            <el-empty v-if="!isPreChoose" description="暂未选择患者的处方" />
            <div v-show="isPreChoose" style="width: 100%">
              <div>
                <span>药品添加</span>
                <el-button
                  size="mini"
                  style="margin-left: 10px"
                  type="success"
                  icon="el-icon-plus"
                  @click="openCreateDrug()"
                />
              </div>
              <el-table
                v-loading="detailLoading"
                :data="detailData"
                stripe
                border
                :default-sort="{ prop: 'id', order: 'asc' }"
                max-height="350"
                min-height="250"
                element-loading-text="正在加载..."
                element-loading-spinner="el-icon-loading"
                element-loading-background="rgba(0, 0, 0, 0.8)"
                style="width: 100%"
              >
                <el-table-column align="center" prop="id" label="详情ID" width="75px" show-overflow-tooltip />
                <el-table-column align="center" prop="medicine_id" label="药品ID" width="75px" show-overflow-tooltip />
                <el-table-column align="center" prop="medicine_name" label="药品名称" width="150px" show-overflow-tooltip>
                  <template slot-scope="scope">
                    <el-tag type="primary" size="mini">{{ scope.row.medicine_name }}</el-tag>
                  </template>
                </el-table-column>
                <el-table-column align="center" prop="description" label="用法" width="125px" show-overflow-tooltip />
                <el-table-column align="center" prop="dosage" label="用量" width="125px" show-overflow-tooltip />
                <el-table-column align="center" prop="frequency" label="频次" width="125px" show-overflow-tooltip />
                <el-table-column align="center" prop="num" label="数量" width="75px" show-overflow-tooltip />
                <el-table-column align="center" prop="lastnum" label="库存量" width="75px" show-overflow-tooltip />
                <el-table-column align="center" label="操作" width="200px">
                  <template slot-scope="scope">
                    <el-button-group>
                      <el-button
                        style="color: #0796b8"
                        type="text"
                        size="mini"
                        @click="openeditDrug(scope.row)"
                      >编辑</el-button>
                      <el-button
                        style="color: #ff0000"
                        type="text"
                        size="mini"
                        @click="deleteDrugs(scope.row)"
                      >删药</el-button>
                    </el-button-group>
                  </template>
                </el-table-column>
              </el-table>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </div>
    <!-- 申请弹窗 -->
    <div>

      <el-dialog
        title="修改处方"
        :visible.sync="editDialogVisible"
        width="80%"
        @close="editDialogVisible = false; resetPre('addPreForm')"
      >
        <el-form ref="addPreForm" :model="addPreForm" :rules="addPreFormRules" width="80%">
          <el-descriptions class="margin-top" :column="3" size="small" border>
            <el-descriptions-item label="描述备注">
              <template>
                <el-form-item prop="description">
                  <el-input v-model="addPreForm.prescription_description" type="textarea" placeholder="请输入描述备注" />
                </el-form-item>
              </template>
            </el-descriptions-item>
          </el-descriptions>
        </el-form>
        <div slot="footer">
          <el-button-group>
            <el-button type="info" icon="el-icon-refresh" @click="resetPre('addPreForm')">重 置</el-button>
            <el-button type="primary" icon="el-icon-close" @click="editDialogVisible = false; resetPre('addPreForm')">取
              消</el-button>
            <el-button type="success" icon="el-icon-edit" @click="editPre('addPreForm')">修 改</el-button>
          </el-button-group>

        </div>
      </el-dialog>

      <el-dialog title="添加药品" :visible.sync="addDrugDialogVisible" width="80%">
        <el-form ref="addDrugForm" :model="addDrugForm" :rules="addDrugFormRules" width="80%">
          <el-descriptions class="margin-top" :column="3" size="small" border>
            <el-descriptions-item label="药品名称">
              <template>
                <el-form-item prop="DrugsName">
                  <div style="display: flex;">
                    <el-tooltip placement="top">
                      <div slot="content">选中并输入关键字进行搜索<br>支持模糊搜索</div>
                      <el-select
                        v-model="drugsValue"
                        filterable
                        size="mini"
                        remote
                        clearable
                        loading-text="拼命加载中..."
                        reserve-keyword
                        placeholder="请输入药品关键词"
                        :remote-method="remoteMethod"
                        :loading="drugsLoading"
                        @change="handleChange111"
                      >
                        <el-option
                          v-for="item in drugsOptions"
                          :key="item.value"
                          :label="item.label"
                          :value="item.label"
                        />
                      </el-select>
                    </el-tooltip>
                    <div style="margin: auto;">当前药品库存</div>
                    <div style="margin: auto;">
                      {{ this.tempLastNum }}
                    </div>
                  </div>

                </el-form-item>
              </template>
            </el-descriptions-item>

          </el-descriptions>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button-group>
            <el-button type="info" icon="el-icon-refresh" @click="resetDrug('addDrugForm')">重 置</el-button>
            <el-button type="primary" icon="el-icon-close" @click="addDrugDialogVisible = false; resetDrug('addDrugForm')">取 消</el-button>
            <el-button type="success" icon="el-icon-plus" @click="createDrug('addDrugForm')">添 加</el-button>
          </el-button-group>

        </div>
      </el-dialog>

      <el-dialog title="修改药品方法" :visible.sync="editDrugDialogVisible" width="80%">
        <el-form ref="editDrugForm" :model="editDrugForm" :rules="editDrugFormRules" width="80%">
          <el-descriptions class="margin-top" :column="3" size="small" border>
            <el-descriptions-item label="用法">
              <template>
                <el-form-item prop="medicine_use_description">
                  <el-input v-model="editDrugForm.medicine_use_description" placeholder="请输入用法" clearable />
                </el-form-item>
              </template>
            </el-descriptions-item>
            <el-descriptions-item label="用量">
              <template>
                <el-form-item prop="medicine_use_dosage">
                  <el-input v-model="editDrugForm.medicine_use_dosage" placeholder="请输入用量" clearable />
                </el-form-item>
              </template>
            </el-descriptions-item>
            <el-descriptions-item label="频次">
              <template>
                <el-form-item prop="medicine_use_frequency">
                  <el-input v-model="editDrugForm.medicine_use_frequency" placeholder="请输入频次" clearable />
                </el-form-item>
              </template>
            </el-descriptions-item>
            <el-descriptions-item label="数量">
              <template>
                <el-form-item prop="medicine_use_num">
                  <el-input-number v-model="editDrugForm.medicine_use_num" :min="1" :max="maxTempLastNum" label="请输入数量" />
                </el-form-item>
              </template>
            </el-descriptions-item>
          </el-descriptions>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button-group>
            <el-button type="info" icon="el-icon-refresh" @click="resetDrug('editDrugForm')">重 置</el-button>
            <el-button type="primary" icon="el-icon-close" @click="editDrugDialogVisible = false; resetDrug('editDrugForm')">取 消</el-button>
            <el-button type="success" icon="el-icon-plus" @click="editDrugs('editDrugForm')">添 加</el-button>
          </el-button-group>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { DocterGetCurrentPaient } from '@/api/medicalEdit'
import { DocterCreateNewDrug, DocterCreateNewPre, DocterDeleteDrug, DocterFinishPre, DocterGetAllDrug, DocterGetAllDrugByPre, DocterGetPaientPreList, DocterUpdateDrug, DocterUpdatePre } from '@/api/prescription'

export default {
  name: 'Prescription',
  data() {
    return {
      presentUser: {},
      tempLastNum: null,
      tempDrugId: 1,
      detailData: [
        {
          DrugsId: 4,
          DrugsName: '阿司匹林',
          DrugsUsage: '口服',
          DrugsDosage: '100mg',
          DrugsFrequency: '每日一次',
          DrugsNum: 10,
          DrugsLastNum: 0
        },
        {
          DrugsId: 5,
          DrugsName: '布洛芬',
          DrugsUsage: '口服',
          DrugsDosage: '200mg',
          DrugsFrequency: '每日两次',
          DrugsNum: 14,
          DrugsLastNum: 2
        },
        {
          DrugsId: 2,
          DrugsName: '盐酸昂丹司琼注射液',
          DrugsUsage: '静脉注射',
          DrugsDosage: '4mg',
          DrugsFrequency: '每4小时一次',
          DrugsNum: 6,
          DrugsLastNum: 0
        },
        {
          DrugsId: 1,
          DrugsName: '盐酸昂丹司琼注射液',
          DrugsUsage: '静脉注射',
          DrugsDosage: '8mg',
          DrugsFrequency: '每8小时一次',
          DrugsNum: 3,
          DrugsLastNum: 2
        },
        {
          DrugsId: 3,
          DrugsName: '盐酸昂丹司琼注射液',
          DrugsUsage: '静脉注射',
          DrugsDosage: '4mg',
          DrugsFrequency: '每6小时一次',
          DrugsNum: 4,
          DrugsLastNum: 2
        }
      ],
      drugsLoading: false,
      drugsValue: '', // 输入框的值
      drugsOptions: [], // 映射给输入框的数据
      drugsList: [], // 输入框下面显示的药品
      drugsStates: [], // 请求获取所有的药品
      detailLoading: false,
      addDrugForm: {
        prescription_id: '', // 对应处方id
        medicine_id: '' // 某个药id，这要与药房的药表一致
      },
      editDrugForm: {
        medicine_use_id: '',
        medicine_use_description: '',
        medicine_use_dosage: '',
        medicine_use_frequency: '',
        medicine_use_num: ''
      },
      addDrugFormRules: {
        // DrugsName: [{ required: true, message: '请输入药品名称', trigger: 'blur' }]
      },
      editDrugFormRules: {

        DrugsUsage: [{ required: true, message: '请输入用法', trigger: 'blur' }],
        DrugsDosage: [{ required: true, message: '请输入用量', trigger: 'blur' }],
        DrugsFrequency: [{ required: true, message: '请输入频次', trigger: 'blur' }],
        DrugsNum: [{ required: true, message: '请输入数量', trigger: 'blur' }]
      },
      addDrugDialogVisible: false,
      editDrugDialogVisible: false,
      chooseTemp: null,
      addDialogVisible: false,
      editDialogVisible: false,
      loading: false,
      isChoose: false,
      isPreChoose: false,
      patientData: [{
        patient_id: 1,
        patient_name: '小明',
        patient_age: '22',
        patient_gender: '男',
        patientPhone: '13340287626',
        patientAddress: '重庆市渝北区',
        patientstatus: '已诊'
      },
      {
        patient_id: 2,
        patient_name: '小红',
        patient_age: '25',
        patient_gender: '女',
        patientPhone: '13840287626',
        patientAddress: '重庆市南岸区',
        patientstatus: '待诊'
      },
      {
        patient_id: 3,
        patient_name: '小刚',
        patient_age: '30',
        patient_gender: '男',
        patientPhone: '13940287626',
        patientAddress: '重庆市沙坪坝区',
        patientstatus: '待诊'
      },
      {
        patient_id: 6,
        patient_name: '小刚',
        patient_age: '30',
        patient_gender: '男',
        patientPhone: '13940287626',
        patientAddress: '重庆市沙坪坝区',
        patientstatus: '待诊'
      },
      {
        patient_id: 5,
        patient_name: '小刚',
        patient_age: '30',
        patient_gender: '男',
        patientPhone: '13940287626',
        patientAddress: '重庆市沙坪坝区',
        patientstatus: '待诊'
      },
      {
        patient_id: 4,
        patient_name: '小刚',
        patient_age: '30',
        patient_gender: '男',
        patientPhone: '13940287626',
        patientAddress: '重庆市沙坪坝区',
        patientstatus: '待诊'
      }],
      choosedPatient: {
        patient_id: '',
        patient_name: '',
        patientPhone: '',
        patient_gender: '',
        patient_age: '',
        appointment_id: ''
      },
      preList: [
      ],
      curPre: {
        PreId: '',
        description: '',
        status: '',
        time: '',
        patient_id: '',
        doctor_id: ''
      },
      addPreForm: {
        prescription_description: '',
        prescription_id: '',
        time: '',
        patient_id: '',
        doctor_id: '',
        prePrice: ''
      },
      addPreFormRules: {
        // time: [{ required: true, message: '请输入处方时间', trigger: 'blur' }]
      }
    }
  },
  computed: {
    maxTempLastNum() {
      return this.tempLastNum !== null ? this.tempLastNum : 0
    }
  },
  mounted() {
    this.presentUser = JSON.parse(localStorage.getItem('User'))
    console.log('初始化医生：', this.presentUser)
    // TODO:用doctorid去获取患者列表
    this.initPatient()
  },
  methods: {
    // 查选的药品的库存
    handleChange111() {
      console.log('搜索药品：', this.drugsValue)
      const tempItem = this.drugsOptions.filter(item => {
        // 检查 item.label 是否存在且为字符串
        return item.label && typeof item.label === 'string' && item.label.indexOf(this.drugsValue) !== -1
      })
      console.log('库存结果：', tempItem)

      if (tempItem.length > 0) {
        this.tempLastNum = parseInt(tempItem[0].lastnum, 10)
        this.tempDrugId = tempItem[0].value
        console.log('库存：', this.tempLastNum)
        if (this.tempLastNum <= 0) {
          this.$notify.error({
            title: '错误',
            message: '库存不足，正在快乐采购中'
          })
        }
      } else {
        console.warn('未找到匹配的药品')
        this.tempLastNum = null
        this.tempDrugId = null
      }
    },
    async openAddPre() { // 新建处方
      const data = {
        patient_id: this.choosedPatient.patient_id,
        doctor_id: this.presentUser.doctor_id,
        appointment_id: this.choosedPatient.appointment_id
      }
      console.log('新建处方：', data)
      // axio
      await DocterCreateNewPre(data)
      this.initPre(this.choosedPatient.patient_id)// 初始化已选择患者的处方申请表信息
    },
    openEditPre(row) { // 打开编辑检验的弹窗
      this.editDialogVisible = true
      this.addPreForm.prescription_id = row.id
      this.addPreForm.prescription_description = row.description || ''

      console.log('编辑申请:', this.addPreForm)
    },
    openeditDrug(row) {
      // console.log('wwwwwwwwwwwwwwwwwwwwwwwa', row)
      this.editDrugDialogVisible = true
      this.editDrugForm.medicine_use_id = row.id
    },
    choosePatient(row) {
      // console.log(row)
      this.$nextTick(() => {
        this.isChoose = true
        this.choosedPatient.patient_name = row.patient_name
        this.choosedPatient.patientPhone = row.patientPhone
        this.choosedPatient.patient_id = row.patient_id
        this.choosedPatient.patient_gender = row.patient_gender
        this.choosedPatient.patient_age = row.patient_age
        this.choosedPatient.appointment_id = row.appointment_id
        console.log('选择患者：', this.choosedPatient)
      })

      // 准备获取当前患者的处方列表
      this.initPre(row.patient_id)// 初始化已选择患者的处方申请表信息
    },
    async initPre(number) {
      // 初始化当前病人的处方列表
      const data = {
        patient_id: number
      }
      // console.log('处方申请列表：', data)
      const res = await DocterGetPaientPreList(data)
      this.preList = res.prescriptions
      // console.log('处方申请列表：', this.applyCheckList)
    },
    preState(row, column) {
      switch (row[column.property]) {
        case 1:
          return '未完成'
        case 2:
          return '已完成'
      }
    },
    // DrugsStates(row, column) {
    //   switch (row[column.property]) {
    //     case 1:
    //       return '缺货'
    //     case 2:
    //       return '可用'
    //   }
    // },
    resetPre(formName) {
      console.log('重置')
      this.addPreForm = { // 注意不能直接为空
        prescription_description: '',
        prescription_id: '',
        time: '',
        patient_id: '',
        doctor_id: '',
        prePrice: ''
      }
      this.$refs[formName].resetFields()
    },
    async editPre(formName) {
      this.$refs[formName].validate(async(valid) => {
        if (valid) {
          const data1 = {
            prescription_id: this.addPreForm.prescription_id,
            prescription_description: this.addPreForm.prescription_description
          }
          console.log('编辑处方：', data1)
          await DocterUpdatePre(data1)
          this.initPre(this.choosedPatient.patient_id)
          this.editDialogVisible = false
          this.$refs[formName].resetFields()
        }
      })
    },
    preDetailEdit(row) {
      this.isPreChoose = true
      this.chooseTemp = row// 记录当前选择的处方
      // console.log('选择的处方：', row)
      this.initDetail(row.id)
      this.initDrugsPre()
    },
    async initDetail(number) {
      const data = {
        prescription_id: number
      }
      console.log('处方id：', data)
      const res = await DocterGetAllDrugByPre(data)
      this.detailData = res.medicine_uses
      console.log('处方详情列表：', this.detailData)
    },
    openCreateDrug() {
      this.drugsValue = ''
      this.tempDrugId = ''
      this.tempLastNum = ''
      this.addDrugDialogVisible = true
      console.log('hihi', this.chooseTemp)
    },
    async createDrug() {
      const data = {
        prescription_id: this.chooseTemp.id,
        medicine_id: this.tempDrugId
      }
      console.log('创建药品：', data)
      await DocterCreateNewDrug(data)
      this.initDetail(this.chooseTemp.id)
      this.addDrugDialogVisible = false
    },
    editDrugs(formName) {
      this.$refs[formName].validate(async(valid) => {
        if (valid) {
          const data1 = {
            medicine_use_id: this.editDrugForm.medicine_use_id,
            medicine_use_dosage: this.editDrugForm.medicine_use_dosage,
            medicine_use_frequency: this.editDrugForm.medicine_use_frequency,
            medicine_use_description: this.editDrugForm.medicine_use_description,
            medicine_use_num: this.editDrugForm.medicine_use_num
          }
          console.log('编辑药品：', data1)
          await DocterUpdateDrug(data1)
          this.editDrugDialogVisible = false
          this.$refs[formName].resetFields()
          this.initDetail(this.chooseTemp.id)
        }
      })
    },
    async deleteDrugs(row) {
      console.log('删除药品：', row)
      const data = {
        medicine_use_id: row.id
      }
      const res = await DocterDeleteDrug(data)
      console.log('删除药品：', res)
      this.initDetail(this.chooseTemp.id)
    },
    remoteMethod(query) {
      if (query !== '') {
        this.drugsLoading = true
        setTimeout(() => {
          this.drugsLoading = false
          this.drugsOptions = this.drugsList.filter(item => {
            return item.label.toLowerCase()
              .indexOf(query.toLowerCase()) > -1
          })
          console.log('搜索结果：', this.drugsOptions)
        }, 200)
      } else {
        this.drugsOptions = []
      }
    },
    // 准备搜索框的药品列表
    async initDrugsPre() {
      // console.log('执行initDrugPre')
      const res = await DocterGetAllDrug()
      // console.log('药品列表：', res)
      this.drugsStates = res.medicines
      // console.log('执行initDrug')
      this.drugsList = this.drugsStates.map(item => {
        // console.log("ITEM = ", item);
        return { value: `${item.id}`, label: `${item.name}`, lastnum: parseInt(item.counts, 10) }
      })
      // console.log('搜索结果：', this.drugsList)
    },
    resetDrug(formName) {
      console.log('重置')
      this.addDrugForm = {}
      this.$refs[formName].resetFields()
    },
    // async addDrugs(formName) {
    //   this.addDrugForm.drugsName = this.drugsValue
    //   this.$refs[formName].validate(async(valid) => {
    //     if (valid) {
    //       this.addDrugForm.prescriptionID = this.chooseTemp.id
    //       this.addDrugForm.drugsID = this.drugsValue
    //       this.addDrugForm.state = 2

    //       console.log('即将添加的药：', this.addDrugForm)
    //       const res = await DocterAddDrug(this.addDrugForm)
    //       console.log('添加药结果：', res)
    //       this.initDetail(this.chooseTemp)// 刷新处方详情
    //       this.resetDrug(formName)// 重置表单
    //       this.addDrugDialogVisible = false
    //     }
    //   })
    // },
    async initPatient() {
      this.loading = true
      const parsedPatient = JSON.parse(localStorage.getItem('patient'))
      console.log('获取当前患者的检查列表：', parsedPatient)
      const data = { appointment_id: parsedPatient.appointment_id }
      const res = await DocterGetCurrentPaient(data)
      console.log('病人列表：', res)
      this.patientData = res
      this.loading = false
    },
    async preDetailDone(row) {
      const data = {
        prescription_id: row.id,
        prescription_status: '已完成'
      }
      await DocterFinishPre(data)
      console.log('结束处方：', data)
      this.initPre(this.choosedPatient.patient_id)
      this.isPreChoose = false
    }

  }
}
</script>

<style></style>
